package org.jeecg.modules.demo.zc.parkingSpace.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.zc.chargingPile.entity.ChargingPile;
import org.jeecg.modules.demo.zc.parkingSpace.entity.ParkingSpace;
import org.jeecg.modules.demo.zc.parkingSpace.service.IParkingSpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * 泊车位
 */
@Api(tags="ParkingSpace")
@RestController
@RequestMapping("/zc/ParkingSpace")
@Slf4j
public class ParkingSpaceController extends JeecgController<ParkingSpace, IParkingSpaceService> {

    @Autowired
    private IParkingSpaceService parkingSpaceService;

    /**
     * 分页列表查询
     *
     * @param parkingSpace
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value="parkingSpace-分页列表查询", notes="parkingSpace-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<ParkingSpace>> queryPageList(ParkingSpace parkingSpace,
                                                     @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                     @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                     HttpServletRequest req) {
        QueryWrapper<ParkingSpace> queryWrapper = QueryGenerator.initQueryWrapper(parkingSpace, req.getParameterMap());
        Page<ParkingSpace> page = new Page<ParkingSpace>(pageNo, pageSize);
        IPage<ParkingSpace> pageList = parkingSpaceService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     *   添加
     *
     * @param parkingSpace
     * @return
     */
    @AutoLog(value = "parkingSpace-添加")
    @ApiOperation(value="parkingSpace-添加", notes="parkingSpace-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody ParkingSpace parkingSpace) {
        parkingSpaceService.save(parkingSpace);
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     *
     * @param parkingSpace
     * @return
     */
    @AutoLog(value = "parkingSpace-编辑")
    @ApiOperation(value="parkingSpace-编辑", notes="parkingSpace-编辑")
    //@RequiresPermissions("trader:trader:edit")
    @PostMapping(value = "/edit")
    public Result<String> edit(@RequestBody ParkingSpace parkingSpace) {
        parkingSpaceService.updateById(parkingSpace);
        return Result.OK("操作成功!");
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "parkingSpace-通过id删除")
    @ApiOperation(value="parkingSpace-通过id删除", notes="parkingSpace-通过id删除")
    //@RequiresPermissions("trader:trader:delete")
    @PostMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name="id",required=true) String id) {
        parkingSpaceService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "parkingSpace-批量删除")
    @ApiOperation(value="parkingSpace-批量删除", notes="parkingSpace-批量删除")
    //@RequiresPermissions("trader:trader:deleteBatch")
    @PostMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.parkingSpaceService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "trader-通过id查询")
    @ApiOperation(value="parkingSpace-通过id查询", notes="parkingSpace-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<ParkingSpace> queryById(@RequestParam(name="id",required=true) String id) {
        ParkingSpace parkingSpace = parkingSpaceService.getById(id);
        if(parkingSpace==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(parkingSpace);
    }

}
